package com.example.myspringtest.alan;

import com.example.myspringtest.handler.InitActProxyHandler;
import com.example.myspringtest.service.*;
import com.example.myspringtest.service.base.BaseHandler;
import com.example.myspringtest.utils.ContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 业务类
 */
@RestController
@RequestMapping("biz")
public class ActBizController implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {

    private List<ActLiveHandler> actLiveHandlers;
    private List<ActRankHandler> actRankHandlers;
    private List<ActSendGiftHandler> actSendGiftHandlers;
    private List<ActTaskHandler> actTaskHandlers;
    private Map<Integer, ActLiveHandler> actLiveHandlerMap;
    private Map<Integer, ActRankHandler> actRankHandlerMap;
    private Map<Integer, ActSendGiftHandler> actSendGiftHandlerMap;
    private Map<Integer, ActTaskHandler> actTaskHandlerMap;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        List<ActLiveHandler> beanLives = ContextUtils.listByType(ActLiveHandler.class);
        List<ActRankHandler> beanRanks = ContextUtils.listByType(ActRankHandler.class);
        List<ActSendGiftHandler> beanGifts = ContextUtils.listByType(ActSendGiftHandler.class);
        List<ActTaskHandler> beanTasks = ContextUtils.listByType(ActTaskHandler.class);

        this.actLiveHandlers = beanLives;
        this.actRankHandlers = beanRanks;
        this.actSendGiftHandlers = beanGifts;
        this.actTaskHandlers = beanTasks;


        actLiveHandlerMap = new HashMap<>();
        for (ActLiveHandler item : beanLives) {
            actLiveHandlerMap.put(item.getId(), item);
        }


        actRankHandlerMap = new HashMap<>();
        for (ActRankHandler item : beanRanks) {
            actRankHandlerMap.put(item.getId(), item);
        }

        actSendGiftHandlerMap = new HashMap<>();
        for (ActSendGiftHandler item : beanGifts) {
            actSendGiftHandlerMap.put(item.getId(), item);
        }

        actTaskHandlerMap = new HashMap<>();
        for (ActTaskHandler item : beanTasks) {
            actTaskHandlerMap.put(item.getId(), item);
        }

    }


    @GetMapping("test")
    public String test() {

        String msg = "测试类";
        if (Objects.nonNull(actLiveHandlers)) {
            for (ActLiveHandler handler : actLiveHandlers) {
                System.out.println(handler.getId() + "--" + handler.getActName());
                handler.handleLiveMsg(msg);
                System.out.println();
                BaseHandler baseHandler = handler.getHandler();
                System.out.println("baseHandler = " + baseHandler);
            }
            System.out.println("====================================");
            System.out.println();
            System.out.println();


        }

        if (Objects.nonNull(actRankHandlers)) {
            for (ActRankHandler handler : actRankHandlers) {
                System.out.println(handler.getId() + "--" + handler.getActName());
                handler.handleRankMsg(msg);
                System.out.println();
                BaseHandler baseHandler = handler.getHandler();
                System.out.println("baseHandler = " + baseHandler.getId());
            }
            System.out.println("====================================");
            System.out.println();
            System.out.println();

        }


        if (Objects.nonNull(actSendGiftHandlers)) {
            for (ActSendGiftHandler handler : actSendGiftHandlers) {
                System.out.println(handler.getId() + "--" + handler.getActName());
                handler.handleGiftMsg(msg);
                System.out.println();
                BaseHandler baseHandler = handler.getHandler();
                System.out.println("baseHandler = " + baseHandler);
            }
            System.out.println("====================================");
            System.out.println();
            System.out.println();

        }


        if (Objects.nonNull(actTaskHandlers)) {
            for (ActTaskHandler handler : actTaskHandlers) {
                System.out.println(handler.getId() + "--" + handler.getActName());
                handler.handleTaskMsg(msg);
                BaseHandler baseHandler = handler.getHandler();
                System.out.println("baseHandler = " + baseHandler);
                System.out.println();
            }
        }


        return "ok";
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ContextUtils.setContext((AnnotationConfigServletWebServerApplicationContext) applicationContext);
        // 首次赋值 applicationContext 的时候 添加初始化的监听器
        ContextUtils.getApplicationContext().addApplicationListener(new InitActProxyHandler());
    }
}
